<?php
/** @var $soft array */
?>
<div class="row" style="margin-top: 10%;">
    <?php foreach ($soft as $key => $value):?>
        <?php if(empty($value['link'])) continue;?>
    <div class="col-sm-6 col-lg-3">
        <div class="well text-center bg-white">
            <img src="<?=$value['logo'];?>">
            <h4><?= $key;?></h4>
            <div class="link-group">
                <?php if(is_array($value['link'])):?>
                <?php foreach ($value['link'] as $link => $linkConfig):?>
                    <a class="<?=$link;?>" href="<?='/soft?id='.$key.'&action='.$link?>"><?=$linkConfig['name'];?></a>
                <?php endforeach;?>
                <?php elseif(is_string($value['link'])):?>
                    <a href="<?=$value['link'];?>" target="_blank">访问</a>
                <?php endif;?>
            </div>
        </div>
    </div>
    <?php endforeach;?>
</div>
<div id="files-conf" class="col-sm-12 col-lg-12 hide">
    <div id="files-menu" class="col-sm-5 col-lg-3">

    </div>
    <div id="files-content" class="col-sm-7 col-lg-9">
        <textarea id="file-content"></textarea>
        <div style="padding: 10px 5px;"><a id="file-save" href="javascript:;" class="btn btn-sm btn-success">保存</a></div>
    </div>
</div>
<script>
    $(function (){

        function setStatus(linkEle,status){
            var softNameEle = linkEle.closest('div.well').find('h4');
            softNameEle.find('span').remove();
            softNameEle.append('<span class="soft-status '+(status?'text-green':'text-red')+'">'+(status?'已运行':'已停止')+'</span>');
            var isFirst = !parseInt(linkEle.data('times') || 0);
            if(linkEle[0].className === 'status' && isFirst){
                linkEle.data('times',1);
                return true;
            }
            return false;
        }

        $('.link-group a').not('[target]').click(function (){
            var self = $(this);
            var url = self.attr('href');
            $.get(url,function (res){
                if(res.code){
                    if(!res.data.type){
                        if(!res.data.code && !res.data.output.length){
                            layer.msg(res.message || '操作成功.');
                            setStatus(self,res.data.status);
                        }else{
                            if(!setStatus(self,res.data.status)){
                                layer.alert('code:'+res.data.code+'</br>signal:'+res.data.signal+'</br>output:<pre class="bg-black text-white">'+res.data.output+'</pre>');
                            }
                        }
                    }else{
                        openConfigFile(res.data.file_data,url);
                    }
                }else{
                    layer.msg(res.message || '操作失败.');
                }
            });
            return false;
        });
        $('.link-group a.status').trigger('click');

        var editor,fileDataGlobal = [];

        function openConfigFile(fileData, url){
            if(!fileData.length){
                layer.msg('暂无配置文件.');
                return;
            }
            fileDataGlobal = fileData;
            var filesConfEle = $('#files-conf');
            var fileContentEle= $('#file-content');
            $('#file-save').data('url',url);
            var fileHtml = '';
            for (var i = 0 ;i< fileData.length; i++){
                fileHtml += '<div data-index="'+i+'" '+(!i?'class="active"':'')+'><a href="javascript:;" title="'+fileData[i].name+'">'+fileData[i].name+'</a></div>';
            }

            $('#files-menu').html('').append(fileHtml);

            fileContentEle.html(fileData[0].content || '');

            layer.open({
                type: 1,
                title: '配置文件',
                area: ['40%', '500px'],
                content: filesConfEle.removeClass('hide'),
                success:function (){
                    if(editor){
                        editor.setValue(fileData[0].content);
                    }else{
                        editor = CodeMirror.fromTextArea(fileContentEle[0], {
                            lineNumbers: true,     // 显示行号
                            indentUnit: 4,         // 缩进单位为4
                            styleActiveLine: true, // 当前行背景高亮
                            matchBrackets: true,   // 括号匹配
                            lineWrapping: true,    // 自动换行
                        });
                        editor.setSize('auto','400px');
                    }

                },
                cancel: function () {
                    filesConfEle.addClass('hide');
                }
            });
        }

        $(document).on('click','#files-menu>div',function (){
            $(this).siblings().removeClass('active');
            $(this).addClass('active');
            editor.setValue(fileDataGlobal[$(this).data('index')].content);
        });

        $(document).on('click','#file-save',function (){
            var index = $('#files-menu>div.active').data('index');
            fileDataGlobal[index].content = editor.getValue();
            var data = fileDataGlobal[index];
            $.post($(this).data('url'),data,function (res){
                layer.msg(res.message || '操作失败.');
            })
        });
    })
</script>